
/**
 * 接口请求封装
 * 支持 get post
 */
import axios from 'axios'
import { Message, Loading } from 'element-ui'
import config from './host'
axios.defaults.baseURL = config.baseUrl
const request = (method = '', url = '', data = {}) => {
    var loadingInstance = Loading.service({ fullscreen: true });
    return new Promise((resolve, reject) => {
        axios.request({
            url,
            method,
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
                'Authorization': sessionStorage.getItem('token')
            },
            data
        }).then(res => {
            const { status } = res
            if (status === 200) {
                const { errCode, errMsg } = res.data
                if (errCode !== 10000) {
                    Message({
                        type: 'error',
                        message: errMsg,
                    })
                }
                resolve(res.data)
            } else {
                // 可以用switch根据code判断显示不同的信息提示
                let msg = statusCode == 404 ? '路径有误' : '信息有误'
                Message({
                    type: 'error',
                    message: msg,
                })
            }
            loadingInstance.close()
        }).catch(err => {
            reject(err)
            loadingInstance.close()
        })
    })
}


const get = (url, data) => request('GET', url, data)
const post = (url, data) => request('POST', url, data)

export default {
    get,
    post
}

